package com.liming.common.interceptors;

import com.liming.common.constant.SystemConstants;
import com.liming.common.util.ThreadLocalUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

/**
 * @author liming
 * @date 2025/1/10
 */
@Slf4j
@Component
public class GiveThreadIdInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String id = request.getHeader(SystemConstants.ID);
        ThreadLocalUtils.set(SystemConstants.ID, id);
        return true;
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        ThreadLocalUtils.remove(SystemConstants.ID);
    }
}
